package com.tianshi.yingxue.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tianshi.yingxue.entity.Video;
import com.tianshi.yingxue.page.Result;
import com.tianshi.yingxue.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @PostMapping("/queryAllPage")
    public Result queryAllPage(@RequestBody Map<String, Integer> params) {
        // 参数提取与校验
        Integer page = params.getOrDefault("page", 1);
        Integer pageSize = params.getOrDefault("pageSize", 2);

        // 验证分页参数有效性
        validatePageParams(page, pageSize);

        // 执行分页查询
        PageInfo<Video> pageInfo = getPageInfo(page, pageSize);

        // 构建响应结果
        return buildPageResult(pageInfo);
    }
    // 参数校验方法
    private void validatePageParams(Integer page, Integer pageSize) {
        if (page == null || page < 1) {
            throw new IllegalArgumentException("页码必须大于0");
        }

        int[] allowedSizes = {2, 3, 5, 10, 15, 20};
        boolean validSize = Arrays.stream(allowedSizes).anyMatch(size -> size == pageSize);
        if (!validSize) {
            throw new IllegalArgumentException("每页条数必须在" + Arrays.toString(allowedSizes) + "范围内");
        }
    }

    // 分页查询封装
    private PageInfo<Video> getPageInfo(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Video> videos = videoService.queryAll();
        return new PageInfo<>(videos);
    }

    // 响应构建封装
    private Result buildPageResult(PageInfo<Video> pageInfo) {
        Map<String, Object> result = new HashMap<>();
        result.put("rows", pageInfo.getList());
        result.put("page", pageInfo.getPageNum());
        result.put("pageSize", pageInfo.getPageSize());
        result.put("total", pageInfo.getTotal());
        result.put("pages", pageInfo.getPages()); // 新增总页数字段

        return Result.success(result);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Video video) {
        videoService.add(video);
        return Result.success("添加成功");
    }

    /**
     * 更新视频信息
     */
    @PostMapping("/update")
    public Result<Boolean> updateVideo(@RequestBody Video video) {
        try {
            if (video.getId() == null) {
                return Result.fail("视频ID不能为空");
            }
            if (video.getTitle() == null || video.getTitle().isEmpty()) {
                return Result.fail("视频标题不能为空");
            }

            videoService.updateVideo(video);
            return Result.success("视频更新成功", true);
        } catch (Exception e) {
            return Result.fail("视频更新失败:" + e.getMessage());
        }
    }
    @PostMapping("/deleteOssFile")
    public Result<Boolean> deleteOssFile(String filePath) {
        try {
            if (filePath == null || filePath.isEmpty()) {
                return Result.fail("文件路径不能为空");
            }
            videoService.deleteOssFile(filePath);
            return Result.success("文件删除成功", true);
        } catch (Exception e) {
            return Result.fail("文件删除失败:" + e.getMessage());
        }
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody Map<String, Integer> params) {
        Integer id = params.get("id");
        videoService.delete(id);
        return Result.success("删除成功");
    }

    @PostMapping("/changeStatus")
    public Result changeStatus(@RequestBody Map<String, Integer> params) {
        Integer id = params.get("id");
        Integer status = params.get("status");
        videoService.changeStatus(id, status);
        return Result.success("状态修改成功");
    }
    @GetMapping("/queryById")
    public Result queryById(@RequestParam Integer id) {
        Video video = videoService.queryById(id);
        return Result.success(video);
    }
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody Map<String, Integer> params) {
        int id = params.get("id");
        int status = params.get("status");
        videoService.updateStatus(id, status);
        return Result.success("状态更新成功");
    }

    // 视频搜索
    @GetMapping("/searchVideo")
    public Result searchVideo(@RequestParam String content) {
        List<Video> videos = videoService.searchVideo(content);
        return Result.success(videos);
    }
}
